<?php
namespace app\index\controller;
use app\index\controller\Commonse;
use think\Db;
use think\Request;
class Config extends Commonse{
    /*  private $da = array(
        'num' => 0,
        'msg' => '参数错误'
    );// 错误信息 */
    public function __construct()
    {
        parent::__construct();
    }

   public function set()
    { // 密码修改
        if (Request::instance()->isPost()) {
            $username =input('post.username', '');
            $password = input('post.password', '');
            $password1 = input('post.password1', '');
            if (empty($username) || empty($password)) {
                $this->error('不能为空');
            }
            if ($password1 != $password) {
                $this->error('两次输入的密码不一致');
            }
            $data = array(
                'username' => $username,
                'password' => md5($password)
            );
            if (Db::name('AdminUser')->where('id=1')->update($data)) {
               session('adminusername',null);
               session('adminid',null);
                $this->success('修改成功',Url('Index/index'));
            } else {
              $this->error('修改失败');
            }
        }
       return $this->fetch();
    }
    public function ym() {
        if(IS_POST){
            $ym=I('post.ym','');
            if(Db::name('Url')->where('id=2')->save(array('url'=>$ym))){
                $this->success('保存成功');
            }else{
                $this->error('保存失败');
            }
    
        }
        $mode=M('Url');
        $info=$mode->where('id=2')->find();
        $this->info=$info;
        $this->fetch();
    }
    function run()
    { // 系统配置修改
        if (Request::instance()->isPost()) {
            $id = input('post.id', 0, 'intval');
            $values = input('post.zc', 0, 'intval');
            $data = array(
                'values' => $values
            );
            if (Db::name('Config')->where('id='.$id)->update($data)) {
                $this->success('修改成功', Url('Config/config'));
            } else {
                $this->error('修改失败');
            }
        }
        $id = input('id', 0, 'intval');
        $info = Db::name('Config')->where('id=' . $id)->find();
        $this->assign('info', $info);
       return $this->fetch();
    }
    function run1()
    { // 系统配置修改
    if (Request::instance()->isPost()) {
        $id = input('post.id', 0, 'intval');
        $values = input('post.zc', 0, 'intval');
        $data = array(
            'values' => $values
        );
        if (Db::name('Config')->where('id='.$id)->update($data)) {
            $this->success('修改成功', Url('Config/config'));
        } else {
            $this->error('修改失败');
        }
    }
    $id = input('id', 0, 'intval');
    $info = Db::name('Config')->where('id=' . $id)->find();
    $this->assign('info', $info);
    return $this->fetch();
    }
   function config()
    { // 系统配置列表
        $config =Db::name('Config');
        $list = $config->where('id=1')->select();
        $list1 = $config->where('id=9')->select();
        $ststem=$config->where("con_type='ststem'")->select();
        $rebate=$config->where("con_type='rebate'")->select();
        $peimoney=$config->where("con_type='order'")->select();
        $this->assign('list',$list);
        $this->assign('list1',$list1);
        $this->assign('ststem',$ststem);
        $this->assign('rebate',$rebate);
        $this->assign('peimoney',$peimoney);
       return $this->fetch();
    }  
    function adduser(){//添加管理员
        if (Request::instance()->isPost()) {
            $username =input('post.username', '');
            $password = input('post.password', '');
            $password1 = input('post.password1', '');
            if (empty($username) || empty($password)) {
                $this->error('不能为空');
            }
            if ($password1 != $password) {
                $this->error('两次输入的密码不一致');
            }
            $data = array(
                'username' => $username,
                'password' => md5($password)
            );
            if (Db::name('AdminUser')->insert($data)) {
                $this->success('添加成功',Url('Index/index'));
            } else {
                $this->error('添加失败');
            }
        }
        return $this->fetch();
    }
    function guanliy(){
        $config =Db::name('AdminUser');
        $list = $config->select();
        $this->assign('list',$list);
        return $this->fetch();
    }
    function add (){
        $config =Db::name('Config');
        if(Request::instance()->isPost()){
            $id=input('id');
            $values=input('values');
            $data=array(
                'values'=>$values
            );
            if($config->where('id='.$id)->update($data)){
                $this->success("修改成功",url('Config/config'));
            }else{
                $this->error('修改失败');
            }
        }
        $id=input('id');
        $list = $config->where('id='.$id)->select();
        $this->assign('list',$list);
        return $this->fetch();
    }
    function delete(){//删除管理员
        $id=input('id');
        $config =Db::name('AdminUser');
        $fbd=$config->where('id='.$id)->find();
        if($fbd){
            if($config->where('id='.$id)->delete()){
                $this->success('删除成功');
            }else{
                $this->error('删除失败');
            }
        }else{
            $this->error('参数错误');
        }
        $list = $config->select();
    }
    function  gamesm(){//游戏说明
        
        if(Request::instance()->isPost()){
             $text=input('post.text');
             
            if(Db::name('Gametext')->where("id=1")->update(array('text'=>$text))){
              
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }
        //显示内容
        $info=Db::name('Gametext')->where('id=1')->find();
        $this->assign('info',$info);
      return  $this->fetch();
    }
    function  kefu(){//佣金说明
    
        if(Request::instance()->isPost()){
             $text=input('post.text');
             
            if(Db::name('Gametext')->where("id=2")->update(array('text'=>$text))){
              
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }
        //显示内容
        $info=Db::name('Gametext')->where('id=2')->find();
        $this->assign('info',$info);
      return  $this->fetch();
    }
   public function kefuer(){
        $db=db('kefu')->where('id=1')->find();
        $this->assign('info',$db['name']);
        $this->assign('src',$db['url']);
        if(Request::instance()->isPost()){
            $data['name']=input('name');
            if(!$data['name']){
                $this->error('内容不能为空');
            }
            $file = request()->file('src');
            if($file){
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                   $data['url']= "/uploads/".date('Ymd')."/".$info->getFilename();
                }else{
                    $this->error($file->getError());
                }
            }
            dump($data);
            if(db('kefu')->where('id=1')->update($data)){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }
        }
        
       
        return  $this->fetch();
    }
    public function upload($src){
        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                // 成功上传后 获取上传信息
                // 输出 jpg
                echo $info->getExtension();
                // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                echo $info->getSaveName();
                // 输出 42a79759f284b767dfcb2a0197904287.jpg
                echo $info->getFilename();
            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }
        return "./uploads/".date('Ymd')."/".$info->getFilename();
    }
   
    
}